<?php


namespace frontend\controllers;


use common\models\ContractImitated;
use Faker\Factory as Faker;
use frontend\facade\Db;
use frontend\facade\Redis;
use frontend\facade\StudentFacade;
use frontend\facade\UserFacade;
use frontend\service\IUserService;
use Yii;
use yii\data\Pagination;
use yii\web\Controller;

class ApiController extends Controller
{
    const DATE_FORMAT = 'Y-m-d H:i:s';

    public function actionIndex()
    {

        $pagination = new Pagination([
            'totalCount'    => 9,
            'pageParam'     => 'page-index',
            'pageSizeParam' => 'page-size',
        ]);


        // Yii::$container->set(Pagination::class,[
        //     'pageParam'      => 'page-index',
        //     'pageSizeParam' => 'page-size',
        // ]);
        $pagination = Yii::$container->get(Pagination::class, [], [
            'totalCount' => 9,
        ]);

        halt($pagination);
    }

    public function actionService()
    {
        /** @var IUserService $service */
        $service = Yii::$container->get(IUserService::class);
        halt($service->findAll(10, 100));
    }

    public function actionService2()
    {
        halt(UserFacade::findAll(40, 10));
    }

    public function actionService3()
    {
        halt(StudentFacade::findAll(60, 20));
    }

    public function actionRedis()
    {
        Yii::$app->redis->set('a', 123123);
        dump(Yii::$app->redis->get('a'));

        Redis::set('b', 'bbbbbbb');
        dump(Redis::get('b'));
    }

    public function actionDb()
    {
        $res = Yii::$app->db->createCommand('SELECT * FROM migration')->queryAll();
        dump($res);
        $res1 = Db::createCommand('SELECT * FROM migration')->queryAll();
        dump($res1);
    }

    public function actionTest()
    {
        dump(date('Y-01-01 00:00:00', strtotime('-30 years')));
    }

    public function actionFaker()
    {
        $faker = Faker::create('zh_CN');
        $cnt = 20;
        for ($i = 0; $i < $cnt; $i++) {
            $times = mt_rand(1, 6);
            for ($j = 0; $j < $times; $j++) {
                $interval[$i][$j] = $faker->dateTimeBetween('-30 years', '+10 years')->format('Y-m-d H:i:s');
            }
            sort($interval[$i]);
        }

        $date = [];
        foreach ($interval as $key => $time) {
            $i = 0;
            $cycleTimes = count($time);
            while ($i < $cycleTimes) {
                $date[$key][$i] = [
                    'StartDate' => $i == 0 ? '1990-01-01 00:00:00' : date(self::DATE_FORMAT, strtotime($time[$i - 1])),
                    'EndDate'   => date(self::DATE_FORMAT, strtotime($time[$i]) - 1),
                ];

                $i++;
            }
            $date[$key][$cycleTimes] = [
                'StartDate' => end($time),
                'EndDate'   => '9999-12-31 12:59:59',
            ];
        }

        $result = [];
        foreach ($date as $k1 => $v1) {
            foreach ($v1 as $v2) {
                $row = $v2;
                $row['UnitId'] = $k1 + 1;
                $row['IsCompetitor'] = mt_rand(1, 100) <= 80 ? 0 : 1;
                $result[] = $row;
            }
        }

        Yii::$app->db->createCommand()->batchInsert(ContractImitated::tableName(), [
            'StartDate',
            'EndDate',
            'UnitId',
            'IsCompetitor',
        ], $result)->execute();
    }
}